package com.codeyourlife.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.codeyourlife.dao.CourseDao;
import com.codeyourlife.entity.Course;
import com.codeyourlife.entity.Lesson;
import com.codeyourlife.entity.User;

@Repository("courseDao")
public class CourseDaoImpl implements CourseDao {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Object> findCourseByEle(String director, String type, String degree, String orderstyle,
			int pageNumber) {
		System.out.println("coursedao处理");
		if (director.equals("*")) {
			director = "is not null";
		} else {
			director = "= '" + director + "'";
		}
		if (type.equals("*")) {
			type = "is not null";
		} else {
			type = "= '" + type + "'";
		}
		if (degree.equals("*")) {
			degree = " is not null";
		} else {
			degree = "= '" + degree + "'";
		}

		List<Course> courseList = new ArrayList<Course>();
		List<Course> queryCourseByEle = new ArrayList<Course>();

		int fixNumber = 5;
		try {
			Session session=this.getSessionFactory().getCurrentSession();
			Query query = session.createQuery("from Course c where (c.director " + director + " and c.type " + type
							+ " and c.degree " + degree + ") ORDER BY " + orderstyle + "  DESC");
			queryCourseByEle = query.list();
			session.close();
			
			int totalNumber = queryCourseByEle.size() / fixNumber;
			if (queryCourseByEle.size() % fixNumber != 0) {
				totalNumber = totalNumber + 1;
			}
			System.out.println("the length of queryCourseByEle = " + queryCourseByEle.size());
			for (int i = (pageNumber - 1) * fixNumber; i < pageNumber * fixNumber; i++) {
				if (i == queryCourseByEle.size()) {
					break;
				}
				courseList.add(queryCourseByEle.get(i));

			}
			ArrayList<Object> arr = new ArrayList();
			System.out.println((int) Math.ceil(queryCourseByEle.size() / fixNumber));
			arr.add("{totalPage:" + totalNumber + "}");
			arr.addAll(courseList);
			return arr;
		} catch (Exception e) {
			System.out.println("Dao==findCourseByEle: " + e.getStackTrace());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Lesson> findCourseLesson(int id) {
		try {
			System.out.println(id);
			Session session=this.getSessionFactory().getCurrentSession();
			Query query = session.createQuery("from Lesson l where l.course_id='" + id+"'");
			return query.list();
		} catch (Exception e) {
			System.out.println("CourseDaoImpl:findCourseLesson " + e.getStackTrace());
		}
		return null;
	}

	public Course findCourseById(int id) {
		Session session=this.getSessionFactory().getCurrentSession();
		Course course = session.load(Course.class, id);
		return course;
	}

	public void addLesson(Lesson lesson) {
		Session session=this.getSessionFactory().getCurrentSession();
		session.save(lesson);
	}
	
	@SuppressWarnings("unchecked")
	public List<Object> courseInfo(int userId, int courseId) {
		// TODO Auto-generated method stub
		
		/*Course course = session.load(Course.class, courseId);*/
		System.out.println("dao层"+userId);
		int isattend = 0;
		int iscollect = 0;
		try {
			Session session=this.getSessionFactory().getCurrentSession();
			Query queryCourse = session.createQuery(
					"from Course c where c.id='"+courseId+"'");
			List<Course> courses= queryCourse.list(); 
			Query  queryUser = session.createQuery("from User u where u.id='"+courses.get(0).getOwner_id()+"'");
			List<User> users = queryUser.list();
			System.out.println(queryCourse.list().get(0).toString());
			Query queryLearning = null;
			Query queryFavCourse = null;
			if(userId!=-1)
			{
				queryLearning = session.createQuery(
						"from UserLearning ul where (ul.course_id='"+courseId+"' and ul.user_id='"+userId+"')");
				queryFavCourse = session.createQuery(
						"from UserFavCourse ufc where (ufc.favcourse_id='"+courseId+"' and ufc.user_id='"+userId+"')");	
			}
			if(queryLearning!=null&&userId!=-1)
			{
				isattend = 1;
				System.out.println(isattend);
			}
			if(queryFavCourse!=null&&userId!=-1)
			{
				iscollect = 1;
				System.out.println(iscollect);
			}
		
			ArrayList<Object> allCourseInfo= new ArrayList<Object>();
			allCourseInfo.add("{isAttend:" + isattend + "}");
			allCourseInfo.add("{isCollect:" + iscollect + "}");
			allCourseInfo.addAll(courses);
			allCourseInfo.addAll(users);
			return allCourseInfo;
		} catch (Exception e) {
			System.out.println("Dao==courseInfo: " + e.getStackTrace());
		}
		return null;
	}
	
}
